package logic

import (
	"context"
	"errors"
	"goods_srv/models"

	"goods_srv/goods"
	"goods_srv/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type GoodsListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGoodsListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GoodsListLogic {
	return &GoodsListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 商品列表
func (l *GoodsListLogic) GoodsList(in *goods.GoodsListRequest) (*goods.GoodsListResponse, error) {
	// todo: add your logic here and delete this line

	var err error

	var g models.Goods
	g.GoodsTitle = in.Title
	goodsList, err := g.GetGoodsList(int(in.Page), int(in.PageSize))
	if err != nil {
		logx.Error("商品列表展示失败")
		return nil, errors.New("商品列表展示失败")
	}

	var goodslist []*goods.GoodsList
	for _, g2 := range goodsList {
		list := goods.GoodsList{
			UserId:     int64(g2.UserId),
			GoodsId:    int64(g2.ID),
			GoodsTitle: g2.GoodsTitle,
			GoodsPrice: float32(g2.GoodsPrice),
			GoodsStock: int64(g2.GoodsStock),
		}
		goodslist = append(goodslist, &list)
	}

	return &goods.GoodsListResponse{
		List: goodslist,
	}, nil
}
